//
//  TestForNotificationDetailVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/17.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "TestForNotificationDetailVC.h"
#import "PutNormolForIPhoneCell.h"

#define PutCellID @"cellid1"

@interface TestForNotificationDetailVC ()
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *contentArray;
@property (nonatomic,strong) UIButton *leftBtn;
@property (nonatomic,strong) UIButton *rightBtn;
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) NSString *time;
@end

@implementation TestForNotificationDetailVC

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self resetNav];
    [self setWebData];
    [self setTableViewFooterView];
}
#pragma mark - Property
- (NSArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = @[@"验收通知单编码",@"通知单编码",@"验收发起部门",@"验收接受部门",@"验收内容",@"验收人",@"验收日期"];
    }
    return _titleArray;
}
- (NSArray *)contentArray{
    if (_contentArray==nil) {
        _contentArray = @[@"超大工程",@"XCG76K100K300102030002",@"交建一局",@"厦门公路局",@"养护一队",@"2017-01-01",@"2017-02-02"];
    }
    return _contentArray;
}
#pragma mark - UI
- (void)resetNav{
    [self setTheBackBtn];
    self.myTitle.text = @"验收通知单";
    [self.tableView registerClass:[PutNormolForIPhoneCell class] forCellReuseIdentifier:PutCellID];
    
}
- (void)setTableViewFooterView{
    
    self.footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 200*WidthRate)];
    self.footerView.backgroundColor = [PublicMethod colorWithHexString:@"#eaeaea" andAlpha:1];
    self.tableView.tableFooterView = self.footerView;
    
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn.layer.cornerRadius = 5;
    self.leftBtn.layer.masksToBounds = YES;
    self.leftBtn.layer.borderColor = [MAIN_Color CGColor];
    self.leftBtn.layer.borderWidth = 1;
    self.leftBtn.backgroundColor = Bai_Color;
    [self.leftBtn setTitleColor:MAIN_Color forState:UIControlStateNormal];
    [self.leftBtn setTitle:@"重拟验收日期" forState:UIControlStateNormal];
    [self.leftBtn addTarget:self action:@selector(resetTime) forControlEvents:UIControlEventTouchUpInside];
    self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:17*ZitiRate];
    [self.footerView addSubview:self.leftBtn];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-120*WidthRate);
        make.height.mas_equalTo(42*WidthRate);
        make.left.mas_equalTo(22*WidthRate);
        make.right.equalTo(self.footerView.mas_centerX).offset(-15*WidthRate);
    }];
    
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightBtn.layer.cornerRadius = 5;
    self.rightBtn.layer.masksToBounds = YES;
    self.rightBtn.backgroundColor = MAIN_Color;
    [self.rightBtn setTitleColor:Bai_Color forState:UIControlStateNormal];
    [self.rightBtn setTitle:@"启动验收" forState:UIControlStateNormal];
    [self.rightBtn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:17*ZitiRate];
    [self.footerView addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-120*WidthRate);
        make.height.mas_equalTo(42*WidthRate);
        make.right.mas_equalTo(-22*WidthRate);
        make.left.equalTo(self.footerView.mas_centerX).offset(15*WidthRate);
    }];
    if (self.state) {
        self.tableView.tableFooterView.hidden = YES;
    }else{
        self.tableView.tableFooterView.hidden = NO;
    }
}

#pragma mark - UI Response
- (void)resetTime{
    //x,y 值无效，默认是居中的
    KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
    
    //配置中心，详情见KSDatePikcerApperance
    
    picker.appearance.radius = 5;
    
    //设置回调
    picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
        
        if (buttonType == KSDatePickerButtonCommit) {
            
            NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            self.time = [formatter stringFromDate:currentDate];
           
            [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
            
            [NetWorkTool resetWithMaintentanceRecordId:self.dic[@"acceptanceNoticeId"] planAcceptanceDate:self.time block:^(id data) {
                [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
                if (data) {
                    if ([data[@"success"]integerValue]) {
                        
                        
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"重拟验证日期成功" animated:YES];
                        [self performSelector:@selector(pop) withObject:nil afterDelay:1];
                
                        
                    }else{
                        if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                            [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                        }else{
                            [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                        }                    }
                }else{
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
                }
            }];

        }
    };
    // 显示
    [picker show];
}
- (void)test{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"确认" message:@"确定要启动验收么?" preferredStyle:UIAlertControllerStyleAlert];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
        
        [NetWorkTool becomeReceiveWithmaintentanceRecordId:self.dic[@"acceptanceNoticeId"] block:^(id data) {
         [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
            if (data) {
                if ([data[@"success"]integerValue]) {
                    
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"启动验收成功" animated:YES];
                    
                    [self performSelector:@selector(pop) withObject:nil afterDelay:1];
                    
                }else{
                    if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                        [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                    }
                }
            }else{
                
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
            }
        }];

    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:actionSheet animated:YES completion:nil];
   
}
- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Data handle
- (void)setWebData{
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [NetWorkTool getAcceptanceNotificationDetailWithID:self.codeId block:^(id data) {
      [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                self.dic = data[@"data"];
                self.time = self.dic[@"planAcceptanceDate"];
                [self.tableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];

}
#pragma mark - Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 7;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45*WidthRate;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
         PutNormolForIPhoneCell *cell = [tableView dequeueReusableCellWithIdentifier:PutCellID];
         cell.titleLab.text = self.titleArray[indexPath.row];
         switch (indexPath.row) {
        case 0:
        {
            cell.contentLab.text = self.dic[@"acceptanceNoticeNo"];
            [cell.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10*WidthRate);
                make.top.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.width.mas_equalTo(100*WidthRate);
            }];
        }
            break;
        case 1:
        {
            cell.contentLab.text = self.dic[@"maintentanceNoticeNo"];
        }
            break;
        case 2:
        {
            cell.contentLab.text = self.dic[@"lanuchDepartmentName"];
        }
            break;
        case 3:
        {
            cell.contentLab.text = self.dic[@"receiveDepartmentName"];
        }
            break;
        case 4:
        {
            cell.contentLab.text = self.dic[@"content"];
        }
            break;
        case 5:
        {
            cell.contentLab.text = self.dic[@"acceptanceUserName"];
        }
            break;
        case 6:
        {
            cell.contentLab.text = self.dic[@"acceptanceDate"];
        }
            break;
       
        default:
            break;
    }
            cell.img.hidden = YES;
            return  cell;

   
}
@end
